(function(Z){
	//Z ScrollBox
	var ScrollBox = function(parameters){
		$.extend(this,parameters);
		this.offset = {
			top:0,
			left:0
		};

		this.position = {
			top : 0,
			left : 0
		};

		this.barWidth = this.barWidth || 15;

		this.build();
	};

	ScrollBox.prototype = {
		build: function(){
			this.targetHeight = this.target.height();
			this.targetWidth = this.target.width();

			this.target.wrapInner($('<div class="z-scroll-container" style="position:absolut;"></div>'));
			this.container = this.target.find('.z-scroll-container:eq(0)');

			if(this.targetHeight > this.height){
				this.target.append($('<div class="z-vertical-bar"><a href="javascript:void(0);"></a></div>'));
				this.verticalBar = this.target.find('.z-vertical-bar');
				this.verticalBlock = this.verticalBar.children('a');
				this.verticalBar.width(this.barWidth);
			}

			if(this.targetWidth > this.width){
				this.target.append($('<div class="z-horizontal-bar"><a href="javascript:void(0);"></a></div>'));
				this.horizontalBar = this.target.find('.z-horizontal-bar');
				this.horizontalBlock = this.horizontalBar.children('a');
				this.horizontalBar.height(this.barWidth);
			}

			this.target.addClass('z-scroll-box');

			this.init();

			this.bindEvent();
		},
		init: function(){
			if(this.targetHeight > this.height){
				this.verticalBar.height(this.height);
				this.verticalBlock.height(this.verticalBlockHeight);
				this.container.height(this.targetHeight);
				this.target.css('padding-right',this.barWidth+'px');
			}else{
				this.container.height(this.targetHeight);
			}

			if(this.targetWidth > this.width){
				this.horizontalBlock.width(this.horizontalBlockWidth);
				this.container.width(this.targetWidth);
				this.target.css('padding-bottom',this.barWidth+'px');
			}else{
				this.container.width(this.targetWidth);
			}

			this.target.height(this.height||this.targetHeight);
			this.target.width(this.width||this.targetWidth);
		},
		bindEvent : function(){
			var self = this,
			startMoveX = false,
			startMoveY = false;
			//监听鼠标滚动事件
			if($.browser.mozilla){
				document.addEventListener('DOMMouseScroll',function(event){
					self.onMouseWheel(self,event);
				},true);
			}else{
				$(this.target).bind('mousewheel',function(event){
					self.onMouseWheel(self,event);
				});
			}

			//监听滑块移动事件
			$(this.verticalBlock).mousedown(function(){
				return !(startMoveY = true);
			});

			$(this.horizontalBlock).mousedown(function(){
				return !(startMoveX = true);
			});

			$(document).mousemove(function(event){
				if(startMoveY){
					self.position.top = event.pageY-self.target.offset().top-self.verticalBlockHeight/2;

					if(self.position.top > (self.height-self.verticalBlockHeight)){
						self.position.top = self.height-self.verticalBlockHeight;
					}

					if(self.position.top < 0){
						self.position.top = 0;
					}

					self.verticalScroll();
				}

				if(startMoveX){
					self.position.left = event.pageX-self.target.offset().left-self.horizontalBlockWidth/2;

					if(self.position.left > self.horizontalBar.width()-self.horizontalBlockWidth){
						self.position.left = self.horizontalBar.width()-self.horizontalBlockWidth;
					}

					if(self.position.left < 0){
						self.position.left = 0;
					}

					self.horizontalScroll();
				}
				return false;
			});

			$(document).mouseup(function(){
				startMoveY = false;
				return startMoveX = false;
			});

			$(this.verticalBar).click(function(event){
				self.position.top = event.pageY-self.target.offset().top-self.verticalBlockHeight/2;

				if(self.position.top > self.verticalBar.height()-self.verticalBlockHeight){
					self.position.top = self.verticalBar.height()-self.verticalBlockHeight;
				}

				if(self.position.top < 0){
					self.position.top = 0;
				}

				self.verticalScroll(true);
			});

			$(this.horizontalBar).click(function(event){
				self.position.left = event.pageX-self.target.offset().left-self.horizontalBlockWidth/2;

				if(self.position.left > self.horizontalBar.width()-self.horizontalBlockWidth){
					self.position.left = self.horizontalBar.width()-self.horizontalBlockWidth;
				}

				if(self.position.left < 0){
					self.position.left = 0;
				}

				self.horizontalScroll(true);
			});
		},
		onMouseWheel : function(self,event){
			var direction = $.browser.mozilla?-event.detail:event.originalEvent.wheelDelta,
			step=20;

			if(self.container.height() > self.height){
				if(direction == -Math.abs(direction)){
					//向下滚动
					if(self.position.top < (self.height-self.verticalBlockHeight)){
						var lastDistance = self.height-self.verticalBlockHeight-self.position.top;
						self.position.top += lastDistance<step?lastDistance:step;
					}
				}else{
					//向上滚动
					if(self.position.top > 0){
						self.position.top -= self.position.top<step?self.position.top:step;
					}
				}
				self.verticalScroll();
			}
		},
		verticalScroll : function(animate){
			if(this.container.height() > this.height){
				if(this.position.top <0){
					this.position.top = 0;
				}

				if(this.position.top > (this.height-this.verticalBlockHeight)){
					this.position.top = this.height-this.verticalBlockHeight;
				}

				$(this.verticalBlock).show().css('top',this.position.top+'px');

				var l = -(this.position.top/(this.height-this.verticalBlockHeight))*(this.container.height()-this.height)+'px';
				if(animate||false){
					this.target.find('.z-scroll-container').animate({
						'top':l
					}, 300);
				}else{
					this.target.find('.z-scroll-container').css('top',l);
				}
			}
		},
		horizontalScroll : function(animate){
			if(this.container.width() > this.width){
				if(this.position.left <0){
					this.position.left = 0;
				}

				if(this.position.left > (this.width-this.horizontalBlockWidth)){
					this.position.left = this.width-this.horizontalBlockWidth;
				}

				$(this.horizontalBlock).show().css('left',this.position.left+'px');

				var l = -(this.position.left/(this.width-this.horizontalBlockWidth))*(this.container.width()-this.width)+'px';
				if(animate||false){
					this.container.animate({
						'left':l
					}, 300);
				}else{
					this.container.css('left',l);
				}
			}
		}
	};
	
	Z.ScrollBox = ScrollBox;
})(window.Z);